﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BTv2.Model
{
    public class SimpleCondition : Condition
    {
        public static string BiggerThanOperator = ">";
        public static string LessThanOperator = "<";
        public static string BiggerThanOrEqualsOperator = ">=";
        public static string LessThanOrEqualsOperator = "<=";
        public static string EqualsOperator = "=";

        public Condition _backup;
        public Sensor _sensor;
        public string _operator;
        public double _value;

        public Condition Backup{

            get { return _backup; }
            set
            {
                if (_backup != value)
                {
                    _backup = value;
                    NotifyPropertyChanged();
                }
            }
        }
        public Sensor Sensor {
            get { return _sensor; }
            set
            {
                if (_sensor != value)
                {
                    _sensor = value;
                    NotifyPropertyChanged();
                }
            }
        }
        public string Operator {
            get { return _operator; }
            set
            {
                if (_operator != value)
                {
                    _operator = value;
                    NotifyPropertyChanged();
                }
            }
        }
        public double Value {
            get { return _value; }
            set
            {
                if (_value != value)
                {
                    _value = value;
                    NotifyPropertyChanged();
                }
            }
        }
        public string SValue { get { return "" + Value; } }

        public SimpleCondition(Sensor s, string op, double value, Condition parent)
        {
            Sensor = s;
            Operator = op;
            Value = value;
            XamlName = "SimpleCondition";
            Parent = parent;
        }
        public SimpleCondition(Sensor s, string op, double value)
        {
            Sensor = s;
            Operator = op;
            Value = value;
            XamlName = "SimpleCondition";
        }

        public SimpleCondition(Sensor s, string op)
        {
            Sensor = s;
            Operator = op;
            XamlName = "SimpleCondition";
        }

        public SimpleCondition(Sensor s)
        {
            Sensor = s;
            XamlName = "SimpleCondition";
        }

        public SimpleCondition()
        {
            XamlName = "SimpleCondition";
        }

        public override string ParseCondition()
        {
            return Sensor.Name + " " + Operator + " " + Value;
        }

        public async override Task<bool> EvaluateCondition()
        {
            if( Operator == BiggerThanOperator){
                return (await Sensor.GetCurrentValue()).Value > Value;

            }else if(Operator == LessThanOperator){
                return (await Sensor.GetCurrentValue()).Value < Value;

            }else if(Operator == BiggerThanOrEqualsOperator){
                return (await Sensor.GetCurrentValue()).Value >= Value;

            }else if(Operator == LessThanOrEqualsOperator){
                return (await Sensor.GetCurrentValue()).Value <= Value;
            }
            else if (Operator == EqualsOperator)
            {
                return (await Sensor.GetCurrentValue()).Value == Value;
            }
            else
            {
               return false;
            }
        }

        public override string ParseSQLCondition()
        {
            if (Operator == BiggerThanOperator)
            {
                return "lastSensorValue(" + Sensor.ID + ") > " + Value;

            }
            else if (Operator == LessThanOperator)
            {
                return "lastSensorValue(" + Sensor.ID + ") < " + Value;

            }
            else if (Operator == BiggerThanOrEqualsOperator)
            {
                return "lastSensorValue(" + Sensor.ID + ") >= " + Value;

            }
            else if (Operator == LessThanOrEqualsOperator)
            {
                return "lastSensorValue(" + Sensor.ID + ") <= " + Value;
            }
            else if (Operator == EqualsOperator)
            {
                return "lastSensorValue(" + Sensor.ID + ") < " + Value;
            }
            else
            {
                return "";
            }
        }

        public override Condition Copy()
        {
            return new SimpleCondition(Sensor, Operator, Value, Parent);
        }
    }
}
